display.setStatusBar(display.HiddenStatusBar)


--baloon
local cuadradoC = require("cuadrado")
local grid = {}

local finished = false
local paths=0
local levelComplete
local gameGroup = display.newGroup( )
local pathText = display.newText(" ", 10, 20,native.systemFont, 10)
pathText:rotate( 90 )
--main text
local _w = display.contentWidth
local _h = display.contentHeight
local _crf = display.CenterReferencePoint
local text = display.newText(" ",200, 100, "Arcade", 100)
text:setReferencePoint(_crf)
text.x = _w * 0.5
text.y = _h * 0.5
text:setTextColor( 138, 43, 226, 255 )
local levelText = display.newText(" ", 10, 200, native.systemFont, 10)
levelText:rotate( 90 )

local frame = 0
local level = 1

local status_init = 0
local status_play = 1
local status_end = 2
local game_status = init

local function clear()
    frame = 0
    for j = 0, 4 do
        for i = 0, 6 do
            gameGroup:remove( grid[i][j] )
            grid[i][j] = nil
            
        end
    end
    paths = 0
    if(text ~= nil )then 
        text.text = "" 
    end
end




local function checkCompleteI(i, j, color,marked)
    if(game_status ~= status_play) then
        return false
    end
    
    if(marked[i][j]==true)then
        return false
    end
    marked[i][j]=true
    --print("check:"..i..":"..j..":"..color)
    if (i == 6 and grid[i][j].color() == color) then
        text.text = "COMPLETED"
        game_status = status_end
        print("YA")
    end
    
    if(i==6)then
        return false
    end
    
    if (i+1<7) then
        if (grid[i + 1][j].color() == color) then
            checkCompleteI(i + 1, j, color,marked)
        end
    end
    
    if (i>0 and j+1<5) then
        if (grid[i][j + 1].color() == color) then
            checkCompleteI(i, j + 1, color,marked)
        end
    end
    
    if (i>0 and j-1>-1) then
        if (grid[i][j - 1].color() == color) then
            checkCompleteI(i, j - 1, color,marked)
        end
    end
end



local function checkPathI(i, j, color,marked)
    if(marked[i][j]==true)then
        return false
    end
    marked[i][j]=true
    --print("check:"..i..":"..j..":"..color)
    if (i == 6 and grid[i][j].contains(color)) then
        paths = paths + 1
        print("Paths:"..paths)
        return 1;
    end
    
    if(i==6)then
        return 0;
    end
    
    if (i+1<7) then
        if (grid[i + 1][j].contains(color)) then
            checkPathI(i + 1, j, color,marked)
        end
    end
    
    if (i>0 and j+1<5) then
        if (grid[i][j + 1].contains(color)) then
            checkPathI(i, j + 1, color,marked)
        end
    end
    
    if (i>0 and j-1>-1) then
        if (grid[i][j - 1].contains(color)) then
            checkPathI(i, j - 1, color,marked)
        end
    end
end

local function checkComplete()
    local marked = {}
    for i = 0, 6 do
        marked[i] = {}
    end
    for j = 0, 4 do
        for i = 0, 6 do
            marked[i][j] = false
        end
    end
    for j = 0, 4 do
        checkCompleteI(0, j, grid[0][j].color(),marked)
    end
end

local function checkPath()
    
    for j = 0, 4 do
        local marked = {}
        for i = 0, 6 do
            marked[i] = {}
        end
        for j = 0, 4 do
            for i = 0, 6 do
                marked[i][j] = false
            end
        end
        checkPathI(0, j, grid[0][j].color(),marked)
    end
end

local function flip()
    for j = 0, 4 do
        for i = 0, 6 do
            grid[i][j].flip()
        end
    end
    
end




local function create()
    for i = 0, 6 do
        grid[i] = {}
    end
    
    for j = 0, 4 do
        for i = 0, 6 do
            grid[i][j] = cuadradoC.newCuadrado(i, j, math.random(1, 4), math.random(1, 2),60)
            gameGroup:insert( grid[i][j] )
        end
    end
    
    checkPath()
    if(paths<1 or  paths>3)then
        clear()
        create()
    end
    pathText.text ="Paths:"..paths
    levelText.text = "LEVEL "..level
    text.text = "LEVEL "..level
    game_status = status_init
end

create()



local function update()
    if(game_status ~= status_play)then
        return false
    end   
    frame = frame + 1
    if (frame == 100) then        
        frame = 0
        print("check")
        checkComplete()
    end
end



Runtime:addEventListener("enterFrame", update)

local function screenTouched(event)     
    if event.phase == "began" then
        if(game_status == status_end)then
            print("status end")
            level = level + 1
            clear()
            create()
            return true
        end
        if(game_status == status_init)then
            print( "status init" )
            flip()  
            text.text = ""
            game_status = status_play
            return true
        end
    end
end

Runtime:addEventListener("touch", screenTouched)